﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using NetTaste;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Logging;
using System.Linq.Expressions;
using NPOI.SS.Formula.Functions;
using DFGK_WMS.Model.Enum;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using static NPOI.HSSF.Util.HSSFColor;
using System.Collections;

namespace DFGK_WMS.Repository
{
    /// <summary>
    /// 出库管理
    /// </summary>
    public class TShippingOrderDetailRepository : BaseRepository<TShippingOrderDetail>, ITShippingOrderDetailRepository
    {
        private readonly IMapper mapper;
        private readonly ITShippingLockCellRepository lockCellRepository;

        public TShippingOrderDetailRepository(DFGK_DB context, IMapper mapper, ITShippingLockCellRepository lockCellRepository) : base(context, mapper)
        {
            this.mapper = mapper;
            this.lockCellRepository = lockCellRepository;
        }

        /// <summary>
        /// 删除出库表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DeleteTShippingOrderDetail(string OrderSn, out string errorMsg)
        {
            errorMsg = string.Empty;
            using var tran = Context.Database.BeginTransaction();
            try
            {
                var deleteModel = Context.TShippingOrderDetails.Where(x => x.OrderSn == OrderSn);
                //var delShip = Context.TShippingLockCells.Where(s => s.OrderDetailId == id);
                if (deleteModel == null || deleteModel.Count() <= 0)
                {
                    errorMsg = "找不到要删除的出库单，单号：" + OrderSn;
                    return false;
                }
                if (deleteModel.Any(s => s.Status != 0))
                {
                    errorMsg = "出库单不是创建状态无法删除，单号：" + OrderSn;
                    return false;
                }
                int lockCellId = 0;
                int? inventoryId = 0;
                var updInvList = new List<TInventory>();
                var lockCellList = Context.TShippingLockCells.Where(x => x.OrdeSn == OrderSn).ToList();
                foreach (var lockCell in lockCellList.ToList())
                {
                    lockCellId = lockCell.Id;
                    inventoryId = lockCell.InventoryId;
                    if (inventoryId <= 0)
                    {
                        continue;
                    }
                    // 修改库存状态
                    var invs = Context.TInventories.Find(inventoryId);
                    if (invs != null)
                    {
                        invs.Status = 1;
                        updInvList.Add(invs);
                        Context.TInventory.UpdateRange(updInvList);
                        if (Context.SaveChanges() <= 0)
                        {
                            errorMsg = "删除出库单时,库存信息修改失败,库存ID为:" + invs.Id;
                            tran.Rollback();
                            return false;
                        }
                    }

                    // 删除TShippingLockCells表数据
                    Context.TShippingLockCells.Remove(lockCell);
                    if (Context.SaveChanges() <= 0)
                    {
                        errorMsg = "删除仓位锁定表数据失败,仓位锁定ID为:" + lockCell.Id;
                        tran.Rollback();
                        return false;
                    }

                }
                // 删除TShippingOrderDetails
                Context.TShippingOrderDetails.RemoveRange(deleteModel);
                if (Context.SaveChanges() <= 0)
                {
                    errorMsg = "删除出库单明细表数据失败，出库单号:" + OrderSn;
                    tran.Rollback();
                    return false;
                }
                tran.Commit();
                return true;

            }
            catch (Exception ex)
            {
                errorMsg = "删除出库单catch:" + ex.Message;
                //事务回滚
                tran.Rollback();
                return false;
            }
        }

        /// <summary>
        /// 修改出库单  多物料版本
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdateTshippingOrderDetailPlus(TShippingOrderDetailPlusDTO models, out string errorMsg)
        {
            errorMsg = "修改了出库单";
            using (var tran = Context.Database.BeginTransaction())
            {
                try
                {
                    var updateModelList = Context.TShippingOrderDetails.Where(x => x.OrderSn == models.OrderSn);
                    if (updateModelList == null || updateModelList.Count() <= 0)
                    {
                        tran.Rollback();
                        errorMsg = "要更新的出库单不存在,出库单号：" + models.OrderSn;
                        return false;
                    }
                    //先把出库单中的明细都删除
                    var delOutOrderId = Context.TShippingOrderDetails.Where(s => s.OrderSn == models.OrderSn).Select(s => s.Id);
                    var delLockCell = Context.TShippingLockCells.Where(s => s.OrdeSn == models.OrderSn); //要删除的所有LockCell
                    if (GetStatus(delLockCell.ToList()) != "创建")
                    {
                        tran.Rollback();
                        errorMsg = "出库单不是创建状态";
                        return false;
                    }
                    if (delLockCell == null || delLockCell.Count() <= 0)
                    {
                        tran.Rollback();
                        errorMsg = "仓位锁定表查询失败";
                        return false;
                    }
                    var updInvId = delLockCell.Select(s => s.InventoryId).ToList();
                    var updInvInLockCelll = Context.TInventories.Where(s => updInvId.Contains(s.Id));//要更改状态的库存
                    if (updInvInLockCelll == null || updInvInLockCelll.Count() <= 0)
                    {
                        tran.Rollback();
                        errorMsg = "库存表查询失败";
                        return false;
                    }
                    foreach (var item in updInvInLockCelll)
                    {
                        item.Status = 1;
                    }
                    Context.UpdateRange(updInvInLockCelll);//修改库存
                    if (Context.SaveChanges() <= 0)
                    {
                        tran.Rollback();
                        errorMsg = "库存状态修改失败";
                        return false;
                    }
                    Context.RemoveRange(updateModelList);//删除出库单
                    if (Context.SaveChanges() <= 0)
                    {
                        tran.Rollback();
                        errorMsg = "出库单删除失败";
                        return false;
                    }
                    //Context.RemoveRange(delLockCell);//仓位锁定表 不知道为什么RemoveRange不管用
                    //if (Context.SaveChanges() <= 0)
                    //{
                    //    tran.Rollback();
                    //    errorMsg = "仓位锁定表删除失败";
                    //    return false;
                    //}
                    foreach (var item in delLockCell)
                    {
                        Context.Remove(item);
                        if (Context.SaveChanges() <= 0)
                        {
                            tran.Rollback();
                            errorMsg = "仓位锁定表删除失败";
                            return false;
                        }
                    }
                    //添加出库单明细 仓位锁定表
                    foreach (var item in models.ShippingOrderDetailPlusLockCellDTOs)
                    {
                        //客户
                        var cust = Context.TCustomers.Find(models.CustomerId);
                        if (cust == null)
                        {
                            tran.Rollback();
                            errorMsg = "找不到客户";
                            return false;
                        }
                        //为了查询物料typeCode
                        var material = Context.TMaterials.FirstOrDefault(m => m.Code == item.MaterialCode);
                        if (material == null)
                        {
                            tran.Rollback();
                            errorMsg = "找不到此物料";
                            return false;
                        }

                        //查找库存
                        var invmter = Context.TInventories.Where(s => s.MaterialCode == item.MaterialCode);
                        if (invmter == null || invmter.Count() <= 0)
                        {
                            tran.Rollback();
                            errorMsg = "库存中没有此物料";
                            return false;
                        }
                        //分配出库仓位
                        var lockCellRes = lockCellRepository.LockCell(item.MaterialCode, item.MaterialNum, out var addLockCellList, out var lockerrorMsg);
                        if (!lockCellRes)
                        {
                            tran.Rollback();
                            errorMsg = lockerrorMsg;
                            return false;
                        }
                        TShippingOrderDetail shippingOrderDetail = new TShippingOrderDetail
                        {
                            OrderSn = models.OrderSn, //同一个订单号
                            CustomerId = cust.Id,  // 拿供应商当客户用！
                            CustomerName = cust.Name,
                            CarCord = models.CarCode,
                            MaterialId = item.MaterialId,
                            MaterialName = item.MaterialName,
                            MaterialTypeCode = material.TypeCode,
                            BillType = models.BillType,
                            WarehouseCode = "",
                            WarehouseName = "",
                            AreaCode = "",
                            AreaName = "",
                            BatchNo = "",
                            UnitName = material.UnitName,
                            Qty = item.MaterialNum,
                            Status = 0,
                            StationName = "",
                            CreateDate = DateTime.Now,
                            CreateUserName = "",
                            CreateUserId = 0
                        };

                        //修改托盘满载率
                        var pallList = Context.TPallets.Where(x => x.PalletBarcode == models.ShippingOrderDetailPlusLockCellDTOs.First().PallBarcode);
                        if (pallList.Count() == 0)
                        {
                            tran.Rollback();
                            return false;
                        }
                        var pallListFirst = pallList.First();
                        pallListFirst.LoadFactor = item.LoadFactor;
                        Context.TPallets.Update(pallListFirst);
                        Context.TShippingOrderDetails.Add(shippingOrderDetail);
                        Context.SaveChanges();
                        //货位锁定明细
                        foreach (var addLockCell in addLockCellList)
                        {
                            var inv = Context.TInventories.Where(s => s.Id == addLockCell.InventoryId).FirstOrDefault();
                            if (inv == null)
                                return false;
                            inv.Status = 4;
                            Context.TInventories.Update(inv);
                            //货位锁定明细
                            TShippingLockCell m = new TShippingLockCell
                            {
                                OrdeSn = models.OrderSn,
                                OrderDetailId = shippingOrderDetail.Id,
                                InventoryId = addLockCell.InventoryId,
                                CellId = addLockCell.CellId,
                                CellCode = addLockCell.CellCode,
                                CellName = addLockCell.CellName,
                                PalletBarcode = addLockCell.PalletBarcode,
                                WhId = addLockCell.WhId,
                                CellDepthIndex = 0,
                                StackingStorageId = addLockCell.StackingStorageId,
                                OutQty = item.KeChuCount,
                                InventoryQty = addLockCell.InventoryQty,
                                MaterialId = item.MaterialId,
                                MaterialName = item.MaterialName,
                                MaterialCode = item.MaterialCode,
                                MaterialKeyId = addLockCell.MaterialKeyId,
                                BatchNo = addLockCell.BatchNo,
                                SmallBatchNo = addLockCell.SmallBatchNo,
                                CreateDate = DateTime.Now,
                                RoutewayId = addLockCell.RoutewayId,
                                IsPicking = false,

                            };
                            Context.TShippingLockCells.Add(m);
                            if (Context.SaveChanges() <= 0)
                            {
                                return false;
                            }
                        }
                    }

                    //无异常提交事务
                    tran.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    errorMsg = "cacth:" + ex.Message;
                    return false;
                }
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdTShippingOrderDetailState(ReceivingOrderDetailDto model)
        {
            var robjs = Context.TShippingOrderDetails.Where(s => s.OrderSn == model.OrderSN).ToList();

            if (robjs != null && robjs.Count() != 0)
            {
                foreach (var robj in robjs)
                {
                    switch (model.state)
                    {
                        case 0:
                            var uobj = Context.TUsers.FirstOrDefault(x =>
                                x.Id == model.NowUserId && x.JobNo == model.JobNo);
                            if (uobj != null)
                            {
                                robj.SubmitUserId = model.NowUserId;
                                robj.SubmitUserName = model.NowUserRealName;
                                robj.SubmitDate = DateTime.Now;

                                //修改为：审核通过
                                robj.Status = 2;
                                Context.TShippingOrderDetails.Update(robj);
                                break;
                            }

                            return false;

                        case 1:
                            break;

                        case 2:
                            robj.ModifyUserId = model.NowUserId;
                            robj.ModifyUserName = model.NowUserRealName;
                            robj.ModifyDate = DateTime.Now;
                            //修改为：作业中
                            robj.Status = 3;
                            Context.TShippingOrderDetails.Update(robj);
                            break;

                        case 3:
                            robj.ModifyUserId = model.NowUserId;
                            robj.ModifyUserName = model.NowUserRealName;
                            robj.ModifyDate = DateTime.Now;
                            //修改为：已完成
                            robj.Status = 4;
                            Context.TShippingOrderDetails.Update(robj);
                            break;

                        default:
                            break;
                    }
                }

                return Context.SaveChanges() > 0;
            }

            return false;
        }

        /// <summary>
        /// 根据物料Id返回批次号和数量
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public BatchNoMaxCount GetMaterialIdById(int materialId, string batchNO = null, int? qualityState = null, string warehouseCode = "", string areaCode = "", int? ownerId = null, int? supplierId = null, string produceFactory = "")
        {
            //数量
            try
            {
                BatchNoMaxCount bc = new BatchNoMaxCount();

                //找出该库存的所有批次号
                var materialKeyQuery = Context.TMaterialKeys;
                var inventoryQuery = Context.TInventories;

                var list = (from mk in materialKeyQuery
                            join inv in inventoryQuery
                            on mk.Id equals inv.MaterialKeyId
                            where mk.MaterialId == materialId
                            select new BatchNoMaxCountDTO(mk.BatchNo, mk.Id)).Distinct().ToList();
                bc.materialKeys = list;

                //仓位状态 货架状态 巷道状态
                var cellQuery = Context.TCells;
                var shelfQuery = Context.TShelves;
                var routewayQuery = Context.TRouteways;
                var TInventoryQuery = (from inv in Context.TInventories
                                       join cell in cellQuery
                                       on inv.LocationCode equals cell.Code
                                       join slf in shelfQuery
                                       on cell.ShelfCode equals slf.Code
                                       join rtw in routewayQuery
                                       on cell.RoutewayId equals rtw.Id
                                       where cell.Status == 1 && slf.Status == 1 && rtw.Status == 1
                                       select inv).ToList();
                //var TInventoryQuery = (from inv in Context.TInventories
                //                       join cell in cellQuery
                //                       on inv.LocationId equals cell.Id
                //                       join slf in shelfQuery
                //                       on cell.ShelfId equals slf.Id
                //                       join rtw in routewayQuery
                //                       on cell.RoutewayId equals rtw.Id
                //                       select inv
                //                      ).ToList();

                var invList = TInventoryQuery.Where(t => t.MaterialId == materialId && t.Status == 1 && t.IsLock == false).ToList();
                if (!string.IsNullOrEmpty(batchNO))
                {
                    invList = invList.Where(t => t.BatchNo == batchNO).ToList();
                }
                if (qualityState != null)
                {
                    invList = invList.Where(t => t.QualityState == qualityState).ToList();
                }
                if (!string.IsNullOrEmpty(warehouseCode))
                {
                    invList = invList.Where(t => t.WarehouseCode == warehouseCode).ToList();
                }
                if (!string.IsNullOrEmpty(areaCode))
                {
                    invList = invList.Where(t => t.AreaCode == areaCode).ToList();
                }
                if (ownerId > 0)
                {
                    invList = invList.Where(t => t.OwnerId == ownerId).ToList();
                }
                if (supplierId > 0)
                {
                    invList = invList.Where(t => t.SupplierId == supplierId).ToList();
                }
                if (!string.IsNullOrEmpty(produceFactory))
                {
                    invList = invList.Where(t => t.ProduceFactory.Contains(produceFactory)).ToList();
                }
                decimal? sum = invList.Sum(t => t.AvailableQty);

                List<int?> status = new List<int?>
                {
                    (int)EmBillStatus.Create,
                    (int)EmBillStatus.Submit,
                    (int)EmBillStatus.Pass,
                };
                //一键出库优先级最高
                var shippingOrderDetailQuery = Context.TShippingOrderDetails.OrderByDescending(t => t.IsOnceOut);
                var slcOdQuery = (from lockCell in Context.TShippingLockCells
                                  join od in shippingOrderDetailQuery
                                  on lockCell.OrderDetailId equals od.Id
                                  select new { lockCell, od }
                                ).ToList();
                var slcOdQueryTest = slcOdQuery.ToList();
                List<TShippingLockCell> lockCells = slcOdQuery.Select(x => x.lockCell).ToList();
                List<TShippingOrderDetail> orderDetails = slcOdQuery.Select(x => x.od).ToList();
                orderDetails = orderDetails.Where(od => od.MaterialId == materialId && status.Contains(od.Status)).ToList();
                lockCells = lockCells.Where(loc => loc.MaterialId == materialId).ToList();
                slcOdQuery = (from lockCell in lockCells
                              join od in orderDetails
                              on lockCell.OrderDetailId equals od.Id
                              select new { lockCell, od }
                        ).ToList();


                //剔除被锁定的库存
                var lockInvIdQuery = (from lockCell in lockCells
                                      join od in orderDetails
                                      on lockCell.OrderDetailId equals od.Id
                                      select lockCell.InventoryId
                            ).Distinct().ToList();
                int lockInvCount = lockInvIdQuery.Count();
                if (lockInvCount > 0)
                {
                    invList = invList.Where(t => !lockInvIdQuery.ToList().Contains(t.Id)).ToList();
                }
                //查出所有匹配的库存
                List<TInventory> TInventoryList = invList.OrderBy(x => x.LocationDepth)
                                                        .ThenBy(x => x.InStockShortDate)
                                                        .ThenByDescending(x => x.AvailableQty)
                                                        .ThenBy(x => x.LocationLineNo)
                                                        .ThenBy(x => x.LocationLayerNo)
                                                        .ThenBy(x => x.LocationColumnNo).ToList();
                //库存可用数量
                decimal totalTInventoryCount = TInventoryList.Sum(t => t.AvailableQty.GetValueOrDefault());
                //当前物料已出库多少

                bc.MaxCount = totalTInventoryCount;
                return bc;

            }
            catch (Exception ex)
            {
                throw ex;
            }

            //BatchNoMaxCount bc = new BatchNoMaxCount();
            ////物料批次
            //var mkobj = Context.TMaterialKeys.FirstOrDefault(x => x.MaterialId == mid);
            ////库存数量
            //var iobj = Context.TInventories.Where(x => x.MaterialId == mid);
            //if (iobj.Count() <= 0)
            //{
            //    return bc;
            //}

            //if (mkobj != null)
            //{
            //    bc.BatchNO.Add(mkobj.BatchNo);
            //}

            //if (iobj != null)
            //{
            //    foreach (var item in iobj.ToList())
            //    {
            //        bc.MaxCount += item.AvailableQty;
            //    }
            //}

            //return bc;
        }

        /// <summary>
        /// 获取商品详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<TShippingGoodsDetail> GetOneTShippingGoodsDetail(int id, int pageIndex, int pageSize, out int totalCount)
        {
            var order = Context.TShippingOrderDetails.Find(id);
            if (order == null)
            {
                totalCount = 0;
                // 订单不存在时返回一个空的列表
                return new List<TShippingGoodsDetail>();
            }
            var details = Context.TShippingGoodsDetails.Where(m => m.OrderDetailId == order.Id).ToList();
            // 获取符合条件的总记录数
            totalCount = details.Count();

            // 对结果进行排序和分页
            var resultList = details.OrderBy(m => m.CreateDate)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();
            return resultList;
        }

        public List<TShippingGoodsDetail> GetByNameShippingGoodsDetail(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var query = Context.TShippingGoodsDetails.AsQueryable();
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(m => m.MaterialName == name);
            }
            totalCount = query.Count();

            // 对结果进行排序和分页
            var resultList = query.OrderBy(m => m.CreateDate)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            return resultList;
        }

        /// <summary>
        /// 出库单显示
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="billType"></param>
        /// <param name="houseid"></param>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<PickDetails> GetPickDetailsList(string? shelvesNo, string? material, int? status, int pageIndex, int pageSize, out int totalCount)
        {
            var shipplist = Context.TShippingOrderDetails.AsNoTracking();
            var shipplocklist = Context.TShippingLockCells.AsNoTracking();
            if (!string.IsNullOrEmpty(shelvesNo))
            {
                shipplist = shipplist.Where(x => x.OrderSn.Contains(shelvesNo));
            }
            if (status.HasValue && status >= 0)
            {
                shipplist = shipplist.Where(x => x.Status == status);
            }
            if (!string.IsNullOrEmpty(material))
            {
                shipplist = shipplist.Where(x => x.MaterialName.Contains(material));
            }
            List<PickDetails> plist = new List<PickDetails>();
            if (!shipplist.Any())
            {
                totalCount = 0;
                return plist;

            }
            foreach (var item in shipplist)
            {
                PickDetails pobj = new PickDetails();
                //出库单明细
                pobj.Id = item.OrderId;
                pobj.PicklistNo = item.OrderSn;
                pobj.PicklistLineNo = item.PurOrderLineNo;
                pobj.BaseUnit = item.BaseUnit;
                pobj.SalesNo = item.DisOrderNo;
                pobj.SalesLine = item.DisOrderLineNo;
                pobj.SupplierName = item.SupplierName;
                pobj.CustomNo = item.CustomNo;
                pobj.Factory = item.ProOrderNo;
                pobj.FactoryName = item.ProduceFactory;
                pobj.Founder = item.CreateUserName;
                pobj.SupplierNo = item.SupplierCode;
                pobj.CreateTime = Convert.ToString(item.CreateDate);
                pobj.WbsCode = item.Erpcode;
                pobj.StorageNo = item.WarehouseCode;
                pobj.Status=item.Status.ToString();
                var shilocklist = shipplocklist.AsNoTracking().Where(x => x.OrderDetailId == item.Id);
                if (shilocklist.Any())
                {
                    foreach (var item2 in shilocklist)
                    {
                        pobj.StockDetailsId = item2.StockDetailsId;
                        pobj.SerialNo = item2.SerialNo;
                        pobj.SerialNumberEnabled = item2.SerialNumberEnabled;
                        pobj.MaterialNo = item2.MaterialCode;
                        pobj.MaterialDesc = item2.MaterialName;
                        pobj.StoreMark = item2.ParentPalletBarcode;
                        pobj.StorageArea = item2.StorageArea;
                        pobj.BinNo = item2.CellCode;
                        pobj.StockLocation = item2.CellName;
                        pobj.QuantityOut = item2.OutQty == null ? 0 : (decimal)item2.OutQty;
                        pobj.BatchNo = item2.BatchNo;
                        pobj.StoreType = item2.StoreType;
                        plist.Add(pobj);
                    }
                }
            }
            totalCount = plist.Count;
            plist = plist.OrderByDescending( x => x.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return plist;
        }
        /// <summary>
        /// 给导出提供数据
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="billType"></param>
        /// <param name="houseid"></param>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TShippingLockCellGetDTO> GetOuHouseToExcel(DateTime? startTime, DateTime? endTime, int? billType, int? houseid, string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var listNoPage = Context.TShippingOrderDetails.AsEnumerable();
            if (billType != null)
                listNoPage = listNoPage.Where(s => s.BillType == billType);
            if (houseid != null)
                listNoPage = listNoPage.Where(s => s.WarehouseId == houseid);
            if (name != null)
                listNoPage = listNoPage.Where(s => s.MaterialName == name);
            if (startTime != null && endTime != null)
                listNoPage = listNoPage.Where(s => s.CreateDate.GetValueOrDefault().Date >= startTime && s.CreateDate.GetValueOrDefault().Date <= endTime);
            var items = listNoPage.OrderByDescending(s => s.CreateDate).GroupBy(s => s.OrderSn);
            var list = items.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            totalCount = items.Count();
            List<TShippingLockCellGetDTO> resList = new List<TShippingLockCellGetDTO>();
            foreach (var item in list)
            {
                var key = item.Key;
                var lockCellList = new List<TShippingLockCellGetDTO>();
                var shippoutList = new List<TShippingOrderDetail>();
                foreach (var value in item)
                {
                    var lockCell = Context.TShippingLockCells.Where(s => s.OrderDetailId == value.Id);
                    foreach (var item1 in lockCell)
                    {
                        var lockCellDTO = new TShippingLockCellGetDTO();
                        lockCellDTO = mapper.Map<TShippingLockCellGetDTO>(item1);
                        lockCellDTO.OrdeSn = key;
                        lockCellDTO.InvCount = GetInvCount(item1.MaterialCode);
                        lockCellDTO.ProductionTime = ExtractDateFromCode(lockCellDTO.SmallBatchNo);
                        lockCellDTO.keChuCount = lockCellRepository.GetKeChuInvCount(lockCellDTO.MaterialCode);
                        lockCellDTO.CarCode = value.CarCord;
                        lockCellDTO.CustomerName = value.CustomerName;
                        lockCellList.Add(lockCellDTO);
                    }
                    shippoutList.Add(value);
                }
                resList.AddRange(lockCellList);
            }
            return resList;
        }

        private string GetStatus(List<TShippingLockCell> shippingGoods)
        {
            if (shippingGoods.All(s => s.IsPicking == true))
            {
                return "已完成";
            }
            else if (shippingGoods.All(s => s.IsPicking == false))
            {
                return "创建";
            }
            else
            {
                return "出库中";
            }
        }

        public UpdGetOutOrder GetOneTShippingOrderDetailPlus(string orderSn)
        {
            var listNoPage = Context.TShippingOrderDetails.AsEnumerable().Where(s => s.OrderSn == orderSn).ToList();
            if (listNoPage.Count() <= 0)
            {
                return null;
            }
            var shippingLockCellList = Context.TShippingLockCells.Where(x => x.OrdeSn == orderSn).ToList();
            var items = listNoPage.OrderByDescending(s => s.CreateDate).GroupBy(s => s.OrderSn);
            UpdGetOutOrder resList = new UpdGetOutOrder();
            List<TShippingOrderDetailPlusLockCellDTO> lockCellDTOs = new List<TShippingOrderDetailPlusLockCellDTO>();

            foreach (var item in shippingLockCellList)
            {
                TShippingOrderDetailPlusLockCellDTO dto = new TShippingOrderDetailPlusLockCellDTO();

                dto.TShippingLockCellsId = item.Id;
                dto.MaterialId = (int)item.MaterialId;
                dto.MaterialName = item.MaterialName;
                dto.MaterialCode = item.MaterialCode;
                var SumCount = Context.TInventories.Where(x => x.MaterialCode == item.MaterialCode).Sum(x => x.AvailableQty);
                dto.MaterialNum = (int)SumCount;
                dto.KeChuCount = item.OutQty;
                dto.PallBarcode = item.PalletBarcode;
                lockCellDTOs.Add(dto);
                resList.shippingOrderDetailPlusLockCellDTOs.Add(dto);
            }
            resList.id = listNoPage.FirstOrDefault().Id;
            resList.OrderSn = listNoPage.FirstOrDefault().OrderSn == null ? "" : listNoPage.FirstOrDefault().OrderSn;
            resList.BillType = listNoPage.FirstOrDefault()?.BillType == null ? 0 : (int)listNoPage.FirstOrDefault().BillType;
            resList.CustomerId = listNoPage.FirstOrDefault()?.CustomerId == null ? 0 : (int)listNoPage.FirstOrDefault()?.CustomerId;
            resList.CarCode = listNoPage.FirstOrDefault()?.CarCord == "" ? "" : listNoPage.FirstOrDefault().CarCord;
            return resList;





            //foreach (var item in items)
            //{
            //    var key = item.Key;
            //    var lockCellList = new List<TShippingLockCell>();
            //    var shippoutList = new List<TShippingOrderDetail>();
            //    foreach (var value in item)
            //    {
            //        var lockCell = Context.TShippingLockCells.Where(s => s.OrderDetailId == value.Id);
            //        foreach (var item1 in lockCell)
            //        {
            //            lockCellList.Add(item1);
            //        }
            //        shippoutList.Add(value);
            //    }

            //    foreach (var lockCell1 in lockCellList)
            //    {
            //        var lockCellobj = new TShippingOrderDetailPlusLockCellDTO
            //        (
            //            lockCell1.Id,
            //            lockCell1.MaterialId.GetValueOrDefault(),
            //            lockCell1.MaterialName,
            //            lockCell1.MaterialCode,
            //            (int)lockCell1.OutQty.GetValueOrDefault()
            //        );
            //        lockCellDTOs.Add(lockCellobj);
            //    }
            //}
            //resList.id = listNoPage[0].Id;
            //resList.OrderSn = listNoPage[0].OrderSn;
            //resList.BillType = listNoPage[0].BillType.GetValueOrDefault();
            //resList.CustomerId = listNoPage[0].CustomerId.GetValueOrDefault();
            //resList.CarCode = listNoPage[0].CarCord;
            //foreach (var item in lockCellDTOs.GroupBy(s => s.MaterialCode))
            //{

            //    foreach (var lockcellitem in shippingLockCellList)
            //    {
            //        var pallBarcodeList = Context.TPallets.Where(x => x.PalletBarcode == lockcellitem.PalletBarcode).ToList();

            //        var lockCellobj = new TShippingOrderDetailPlusLockCellDTO
            //        (
            //            item.First().TShippingLockCellsId,
            //            item.First().MaterialId,
            //            item.First().MaterialName,
            //            item.First().MaterialCode,
            //            item.Sum(s => s.MaterialNum),
            //            lockCellRepository.GetKeChuInvCount(item.First().MaterialCode),
            //            pallBarcodeList.First().PalletBarcode,
            //            pallBarcodeList.First().LoadFactor
            //        );
            //        resList.shippingOrderDetailPlusLockCellDTOs.Add(lockCellobj);
            //    }


            //}
            //resList.shippingOrderDetailPlusLockCellDTOs = resList.shippingOrderDetailPlusLockCellDTOs
            //    .DistinctBy(x => new { x.MaterialCode, x.PallBarcode })
            //    .ToList();

            //return resList;
        }

        private decimal? GetInvCount(string materialCode)
        {
            return Context.TInventories.Where(s => s.MaterialCode == materialCode).ToList().Sum(s => s.AvailableQty);
        }
        /// <summary>
        /// 小批次号拆解
        /// </summary>
        /// <param name="bacNo">小批次号</param>
        /// <returns></returns>
        private int ExtractDateFromCode(string bacNo)
        {
            if (string.IsNullOrEmpty(bacNo))
            {
                return 0;
            }
            if (bacNo.Length < 12)
            {
                return 0;
            }
            string time = bacNo.Substring(2, 8);
            bool containsLetter = time.Any(char.IsLetter);
            if (containsLetter)
            {
                return 0;
            }
            return Convert.ToInt32(time);
        }

        /// <summary>
        /// 出库任务下传
        /// </summary>
        /// <param name="pickDetailsList"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool ShippingTaskDownload(List<PickDetails> pickDetailsList, out string message)
        {
            using (var tran = Context.Database.BeginTransaction())
            {
                try
                {
                    decimal count = 0;
                    message = "";
                    var existingOrderIds = Context.TShippingOrderDetails.AsNoTracking().Where(x => pickDetailsList.Select(s => s.Id).Distinct().Contains(x.OrderId)).Select(x => x.OrderId).ToList();
                    var inventoryobj1 = Context.TInventory.AsQueryable();
                    foreach (var p in pickDetailsList)
                    {
                        if (existingOrderIds.Contains(p.Id))
                        {
                            message += p.Id + "存在重复";
                            continue;
                        }
                        #region 许佳杰代码
                        //var shippingOrder = Context.TShippingOrderDetails.FirstOrDefault(x=>x.OrderSn == p.Id);
                        //var pickDetails = pickDetailsList.Where(x => x.Id == p.Id);
                        //var s = new TShippingOrderDetail();
                        //var shippinglock = new TShippingLockCell();
                        //if (shippingOrder!=null || pickDetails.Count() > 1)
                        //{
                        //    //shippingGoods.OrderDetailId = shippingOrder.Id;
                        //    //shippingGoods.OrderDetailNo = p.Id;
                        //    //shippingGoods.StockDetailsId = p.StockDetailsId;
                        //    //shippingGoods.MaterialCode = p.MaterialNo;
                        //    //shippingGoods.MaterialName = p.MaterialDesc;
                        //    //shippingGoods.StockLocation = p.StockLocation;
                        //    //shippingGoods.SupplierNo = p.SupplierNo;
                        //    //shippingGoods.SupplierName = p.SupplierName;
                        //    //shippingGoods.Factory = p.Factory;
                        //    //shippingGoods.FactoryName = p.FactoryName;
                        //    //shippingGoods.BatchNo = p.BatchNo;
                        //    //shippingGoods.PicklistLineNo = p.PicklistLineNo;
                        //    //shippingGoods.StoreMark = p.StoreMark;
                        //    //shippingGoods.Founder = p.Operator;
                        //    //shippingGoods.CreateDate = Convert.ToDateTime(p.CreateDate);
                        //    //shippingGoods.StorageNo = p.StorageNo;
                        //    //shippingGoods.StorageArea = p.StorageArea;
                        //    //shippingGoods.BinNo = p.BinNo;
                        //    //shippingGoods.SerialNumberEnabled = p.SerialNumberEnabled;
                        //    //shippingGoods.QuantityOut = p.QuantityOut;
                        //    //shippingGoods.SerialNo = p.SerialNo;
                        //    //shippingGoods.SupplierNo = p.SupplierNo;
                        //    //shippingGoods.WbsCode = p.WbsCode;
                        //    //shippingGoods.StoreType = p.StoreType;
                        //    //shippingGoods.BaseUnit = p.BaseUnit;
                        //    //Context.TShippingGoodsDetails.Add(shippingGoods);
                        //    //continue;
                        //}
                        #endregion
                        count += p.QuantityOut;
                        var s = new TShippingOrderDetail();
                        var shippinglock = new TShippingLockCell();
                        //出库单明细
                        s.OrderId = p.Id;
                        s.OrderSn = p.PicklistNo;
                        s.PurOrderLineNo = p.PicklistLineNo;
                        s.Mblnr = p.SapOrderNo;
                        s.Mjahr = p.SapOrderLineNo;
                        s.BaseUnit = p.BaseUnit;
                        s.DisOrderNo = p.SalesNo;
                        s.DisOrderLineNo = p.SalesLine;
                        s.SupplierName = p.SupplierName;
                        s.CustomNo = p.CustomNo;
                        s.ProOrderNo = p.Factory;
                        s.WarehouseCode = p.StorageNo;
                        s.WarehouseName = "中煤科院仓库";
                        s.WarehouseId = 2;
                        s.ProduceFactory = p.FactoryName;
                        s.CreateUserName = p.Founder;
                        s.SupplierCode = p.SupplierNo;
                        s.CreateDate = Convert.ToDateTime(p.CreateTime);
                        s.Erpcode = p.WbsCode;
                        s.MaterialCode = p.MaterialNo;
                        s.MaterialName = p.MaterialDesc;
                        s.Qty = p.QuantityOut;

                        Context.TShippingOrderDetails.Add(s);
                        Context.SaveChanges();

                        //出库货物单明细                    
                        shippinglock.OrderDetailId = s.Id;
                        shippinglock.StockDetailsId = p.StockDetailsId;
                        shippinglock.OrdeSn = p.PicklistNo;
                        shippinglock.SerialNo = p.SerialNo;
                        shippinglock.SerialNumberEnabled = p.SerialNumberEnabled;
                        shippinglock.MaterialCode = p.MaterialNo;
                        shippinglock.MaterialName = p.MaterialDesc;
                        shippinglock.ParentPalletBarcode = p.StoreMark;
                        shippinglock.IsPicking = false;
                        shippinglock.StorageArea = p.StorageArea;
                        //托盘条码-必要
                        var inventoryobj = inventoryobj1.FirstOrDefault(x => x.MaterialCode == p.MaterialNo && x.Status == 1 && x.LocationCode == p.BinNo && x.AvailableQty >= p.QuantityOut);
                        if (inventoryobj != null)
                        {
                            shippinglock.PalletBarcode = inventoryobj.PalletBarcode;
                            shippinglock.InventoryId = inventoryobj.Id;
                            shippinglock.CellId = inventoryobj.LocationId;
                            shippinglock.CellDepthIndex = 0;
                            shippinglock.CellCode = inventoryobj.LocationCode;
                            shippinglock.MaterialId = inventoryobj.MaterialId;
                            shippinglock.InventoryQty = inventoryobj.AvailableQty;
                            inventoryobj.IsLock=true;
                            Context.TInventory.Update(inventoryobj);
                        }
                        else
                        {
                            message += "未在库存中找到此物料：" + p.MaterialNo + " 或因库存数量不足！";
                            return false;
                        }
                        shippinglock.CellName = p.StockLocation;
                        shippinglock.OutQty = p.QuantityOut;
                        shippinglock.BatchNo = p.BatchNo;
                        shippinglock.StoreType = p.StoreType;
                        Context.TShippingLockCells.Add(shippinglock);
                        existingOrderIds.Add(p.Id);
                       
                    }
                    //提交保存
                    Context.SaveChanges();
                    tran.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    tran.Rollback();
                    return false;
                    throw;
                }
            }
        }

        /// <summary>
        /// 出库单回传
        /// </summary>
        /// <param name="picks"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<PickDetails> BackShippingOrder()
        {
            List<PickDetails> plist = new List<PickDetails>();
            //出库单
            var sorderlist = Context.TShippingOrderDetails.AsNoTracking().Where(x => x.Status == 4 && x.OrderSn.Contains("PL") && x.OrderId.Length>0).OrderBy(x => x.CreateDate);
            if (sorderlist.Any())
            {
                foreach (var item in sorderlist)
                {
                    PickDetails pobj = new PickDetails();
                    //出库单明细
                    pobj.Id = item.OrderId;
                    pobj.PicklistNo = item.OrderSn;
                    pobj.PicklistLineNo = item.PurOrderLineNo;
                    pobj.BaseUnit = item.BaseUnit;
                    pobj.SalesNo = item.DisOrderNo;
                    pobj.SalesLine = item.DisOrderLineNo;
                    pobj.SupplierName = item.SupplierName;
                    pobj.CustomNo = item.CustomNo;
                    pobj.Factory = item.ProOrderNo;
                    pobj.FactoryName = item.ProduceFactory;
                    pobj.Founder = item.CreateUserName;
                    pobj.SupplierNo = item.SupplierCode;
                    pobj.CreateTime = Convert.ToString(item.CreateDate);
                    pobj.WbsCode = item.Erpcode;
                    pobj.StorageNo = item.WarehouseCode;

                    //出库单明细
                    var sloclist = Context.TShippingLockCells.AsNoTracking().Where(x => x.OrderDetailId == item.Id);
                    if (sloclist.Any())
                    {
                        foreach (var item2 in sloclist)
                        {

                            //出库货物单明细
                            //立体库库存id-shippinglock.InventoryId = p.StockDetailsId;
                            //shippinglock.OrderDetailId = s.Id;
                            pobj.StockDetailsId = item2.StockDetailsId;
                            pobj.PicklistNo = item2.OrdeSn;
                            pobj.SerialNo = item2.SerialNo;
                            pobj.SerialNumberEnabled = item2.SerialNumberEnabled;
                            pobj.MaterialNo = item2.MaterialCode;
                            pobj.MaterialDesc = item2.MaterialName;
                            pobj.StoreMark = item2.ParentPalletBarcode;
                            pobj.StorageArea = item2.StorageArea;
                            pobj.BinNo = item2.CellCode;
                            pobj.StockLocation = item2.CellName;
                            pobj.QuantityOut = item2.OutQty == null ? 0 : (decimal)item2.OutQty;
                            pobj.BatchNo = item2.BatchNo;
                            pobj.StoreType = item2.StoreType;
                            plist.Add(pobj);
                        }
                    }

                }
                return plist;
            }
            return plist;
        }

        /// <summary>
        /// 获取出库物料
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<ShippingSelectMaterialDto> GetInventoryByMaterialAll(int pageIndex, int pageSize, string? mname, out int count)
        {
            var list = Context.TInventories.AsQueryable().Where(x=>x.Status==1 && x.IsLock==false);
            var pallist = Context.TPallets.AsNoTracking();
            if (mname != null)
            {
                list = list.Where(i => i.MaterialName.Contains(mname) || i.MaterialCode.Contains(mname));
            }
            List<ShippingSelectMaterialDto> shippingSelectMaterialDtos = new List<ShippingSelectMaterialDto>();

            foreach (var item in list)
            {
                if (pallist.Count() == 0)
                {
                    count = 0;
                    return shippingSelectMaterialDtos;
                }
                ShippingSelectMaterialDto dto = new ShippingSelectMaterialDto()
                {
                    InventoryId = item.Id,
                    Code = item.MaterialCode,
                    Name = item.MaterialName,
                    UnitName = item.UnitName,
                    InvCount = item.AvailableQty,
                    PallBarcode = item.PalletBarcode,
                    LoadFactor = pallist.Where(x => x.PalletBarcode == item.PalletBarcode).FirstOrDefault().LoadFactor ?? 0,
                    LocationCode = item.LocationCode,
                };
                shippingSelectMaterialDtos.Add(dto);
            }
            shippingSelectMaterialDtos = shippingSelectMaterialDtos.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            count = list.Count();
            return shippingSelectMaterialDtos;
        }

        /// <summary>
        /// 出库回传修改状态
        /// </summary>
        /// <param name="ridlist"></param>
        /// <returns></returns>
        public bool UpdBackShippingState(List<string> ridlist)
        {
            foreach (var rid2 in ridlist)
            {
                var robj = Context.TShippingOrderDetails.AsNoTracking().FirstOrDefault(x => x.OrderId == rid2);
                if (robj != null)
                {
                    robj.Status = 5;
                    Context.TShippingOrderDetails.Update(robj);
                }
            }
            Context.SaveChanges();
            return true;
        }

        /// <summary>
        /// 添加出库单
        /// </summary>
        /// <param name="model"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddTShippingOrderDetail(TShippingOrderDetail model, out string message)
        {
            using (var tran = Context.Database.BeginTransaction())          
                try
                {
                    var invobjlist = Context.TInventories.AsNoTracking().Where(x=>x.Status==1);
                    if(!invobjlist.Any())
                    {
                        message = "暂时没有可用库存！";
                        tran.Rollback();
                        return false;
                    }
                    string supname = "";
                    string supcode = "";
                    string cusname = "";
                    string cuscode = "";
                    message = "";
                    //供应商信息
                    if (model.SupplierId != null)
                    {
                        var sobj = Context.TSuppliers.FirstOrDefault(s => s.Id == model.SupplierId);
                        if (sobj != null)
                        {
                            supname = sobj.Name;
                            supcode=sobj.Code;
                        }
                        else
                        {
                            message = "供应商不存在或者已被禁用！";
                            tran.Rollback();
                            return false;
                        }
                    }
                    if (model.CustomerId.GetValueOrDefault() > 0)
                    {
                        var cobj = Context.TCustomers.FirstOrDefault(s => s.Id == model.CustomerId);
                        if (cobj != null)
                        {
                            //m.SupplierName = sobj.Name;
                            cusname = cobj.Name;
                            cuscode = cobj.Code;
                        }
                        else
                        {
                            message = "客户不存在或者已被禁用！";
                            tran.Rollback();
                            return false;
                        }
                    }
                    model.WarehouseId = 2;
                    model.WarehouseCode = "W01";
                    model.WarehouseName = "中煤科院仓库";
                    model.Status = 0;
                    model.PickedQty = 0;
                    model.ShippedQty = 0;
                    model.CreateDate = DateTime.Now;
                    model.ProduceFactory = "中煤科工西安研究院（集团）有限公司物资采供工厂";
                    model.PurOrderLineNo = model.OrderSn;
                    model.ProOrderNo = "8100";
                    model.BaseUnit = "EA";
                    model.SupplierName = supname;
                    model.SupplierCode= supcode;
                    model.CustomerName = cusname;
                    model.CustomNo= cuscode;
                    model.CreateDate=DateTime.Now;
                    foreach (var item in model.LockCelllist)
                    {
                       
                        TInventory invobj = new TInventory();
                        invobj = invobjlist.FirstOrDefault(x => x.Id == item.InventoryId);
                        if(invobj==null)
                        {
                            message = "库存信息有误！";
                            tran.Rollback();
                            return false;
                        }
                        invobj.IsLock = true;
                        Context.TInventories.Update(invobj);
                        model.MaterialId = item.MaterialId == null ? 0 : Convert.ToInt32(item.MaterialId);
                        model.MaterialName = item.MaterialName;
                        model.MaterialCode = item.MaterialCode;
                        model.Qty = item.OutQty;
                        TShippingOrderDetail shiporderobj = new TShippingOrderDetail();
                        shiporderobj = Mapper.Map<TShippingOrderDetail>(model);
                        Context.TShippingOrderDetails.Add(shiporderobj);
                        Context.SaveChanges();
                        item.OrderDetailId = shiporderobj.Id;
                        item.OrdeSn = shiporderobj.OrderSn;
                        item.CellId = invobj.LocationId;
                        item.CellCode = invobj.LocationCode;
                        item.CellName = invobj.LocationName;
                        item.PalletBarcode = invobj.PalletBarcode;
                        item.MaterialId = invobj.MaterialId;
                        item.MaterialCode= invobj.MaterialCode;
                        item.MaterialName= invobj.MaterialName;
                        item.BatchNo= invobj.BatchNo;
                        item.InventoryQty = invobj.AvailableQty;
                        item.CreateDate = DateTime.Now;
                        //if(item.OutQty==0)
                        //{
                        //    message = "出库数量不能为0！";
                        //    tran.Rollback();
                        //    return false;
                        //}
                        if (item.OutQty > item.InventoryQty)
                        {
                            message = "出库数量不能大于库存数量！";
                            tran.Rollback();
                            return false;
                        }
                        if (item.OutQty % 1 != 0)
                        {
                            message = "出库数量不能是小数";
                            tran.Rollback();
                            return false;
                        }
                        item.IsPicking = false;
                        Context.TShippingLockCells.Add(item);
                    }
                    tran.Commit();
                    return Context.SaveChanges()>0;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    tran.Rollback();
                    return false;
                    throw;
                }
            
        }

        #region 弃用
        //private string GetStatus(List<TShippingLockCellGetDTO> shippingGoods)
        //{
        //    if (shippingGoods.All(s => s.IsPicking == true))
        //    {
        //        return "已完成";
        //    }
        //    else if (shippingGoods.All(s => s.IsPicking == false))
        //    {
        //        return "创建";
        //    }
        //    else
        //    {
        //        return "出库中";
        //    }
        //}
        ///// <summary>
        ///// 添加出库管理
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public bool AddTShippingOrderDetail2(TShippingOrderDetail model)
        //{
        //    using (var tran = Context.Database.BeginTransaction())
        //    {
        //        try
        //        {
        //            //订单号
        //            //单据类型
        //            //合同号
        //            //物料分类 优先级为顶级 默认为0
        //            //物料名称
        //            var material = Context.TMaterials.Find(model.MaterialId);
        //            if (material != null)
        //            {
        //                model.MaterialCode = material.Code;
        //                model.MaterialName = material.Name;
        //                model.MaterialTypeCode = material.TypeCode;
        //                model.UnitId = material.UnitId;
        //                model.UnitName = material.UnitName;
        //            }

        //            //物料批次信息
        //            // 根据 MaterialId 和 MaterialName 查询对应的物料批次记录
        //            var materialkey = Context.TMaterialKeys.FirstOrDefault(m =>
        //                m.MaterialId == model.MaterialId && m.MaterialName == model.MaterialName);
        //            if (materialkey != null)
        //            {
        //                model.BatchNo = materialkey.BatchNo;
        //            }

        //            //库房信息
        //            var house = Context.TWarehouses.Find(model.WarehouseId);
        //            if (house != null)
        //            {
        //                model.WarehouseCode = house.Code;
        //                model.WarehouseName = house.Name;

        //                // 根据 AreaName 和 WarehouseId 查询对应的库区记录
        //                var area = Context.TAreas.FirstOrDefault(m =>
        //                    m.Name == model.AreaName && m.WhId == model.WarehouseId);
        //                if (area != null)
        //                {
        //                    model.AreaCode = area.Code;
        //                }
        //            }

        //            //出库站台
        //            var station = Context.TStations.Find(model.StationId);
        //            if (station != null)
        //            {
        //                model.StationName = station.Name;
        //            }

        //            //出库数默认为0
        //            model.PickedQty = model.Qty;
        //            model.ShippedQty = 0;
        //            model.Status = 0;
        //            //部门
        //            var userobj = Context.TUsers.Find(model.NowUserId);
        //            if (userobj != null)
        //            {
        //                var deptobj = Context.TDepartments.Find(userobj.DepartmentId);
        //                if (deptobj != null)
        //                {
        //                    model.DeptName = deptobj.DepartmentName;
        //                    model.DeptCode = deptobj.DepartmentCode;
        //                }
        //            }

        //            #region 审计字段

        //            model.CreateUserId = model.NowUserId;
        //            model.CreateUserName = model.NowUserRealName;
        //            model.CreateDate = DateTime.Now;
        //            model.ModifyUserId = model.NowUserId;
        //            model.ModifyUserName = model.NowUserRealName;
        //            model.ModifyDate = DateTime.Now;
        //            model.AuditUserId = model.NowUserId;
        //            model.AuditUserName = model.NowUserRealName;
        //            model.AuditDate = DateTime.Now;
        //            model.SubmitUserId = model.NowUserId;
        //            model.SubmitUserName = model.NowUserRealName;
        //            model.SubmitDate = DateTime.Now;

        //            #endregion 审计字段

        //            //供应商
        //            var supper = Context.TSuppliers.Find(model.SupplierId);
        //            if (supper != null)
        //            {
        //                model.SupplierName = supper.Name;
        //            }

        //            //货主
        //            var owner = Context.TOwners.Find(model.OwnerId);
        //            if (owner != null)
        //            {
        //                model.OwnerName = owner.Name;
        //            }

        //            Context.TShippingOrderDetails.Add(model);
        //            Context.SaveChanges();



        //            //货位锁定明细
        //            foreach (var item in model.LockCell)
        //            {
        //                var inv = Context.TInventories.Where(s => s.Id == item.InventoryId).FirstOrDefault();
        //                inv.Status = 4;
        //                //item.AvailableQty = item.AvailableQty - item.OutQty;
        //                Context.TInventories.Update(inv);
        //                //货位锁定明细
        //                TShippingLockCell m = new TShippingLockCell();
        //                m.OrderDetailId = model.Id;
        //                m.InventoryId = item.InventoryId;
        //                m.CellId = item.CellId;
        //                m.CellCode = item.CellCode;
        //                m.CellName = item.CellName;
        //                m.PalletBarcode = item.PalletBarcode;
        //                m.WhId = model.WarehouseId;
        //                m.CellDepthIndex = 0;
        //                //组垛Id不知道
        //                m.StackingStorageId = 0;
        //                m.OutQty = item.OutQty;
        //                m.InventoryQty = item.InventoryQty;
        //                m.MaterialId = item.MaterialId;
        //                m.MaterialName = item.MaterialName;
        //                m.MaterialCode = item.MaterialCode;
        //                m.MaterialKeyId = item.MaterialKeyId;
        //                m.BatchNo = item.BatchNo;
        //                m.CreateDate = DateTime.Now;
        //                m.RoutewayId = item.RoutewayId;
        //                Context.TShippingLockCells.Add(m);

        //                // 添加货品表
        //                TShippingGoodsDetail product = new TShippingGoodsDetail();
        //                product.OrderDetailId = model.Id;
        //                product.MaterialId = item.MaterialId;
        //                product.MaterialCode = item.MaterialCode;
        //                product.MaterialName = item.MaterialName;
        //                product.Status = 0;
        //                product.PalletBarcode = item.PalletBarcode;
        //                Context.TShippingGoodsDetails.Add(product);

        //                Context.SaveChanges();
        //            }

        //            //无异常提交事务
        //            tran.Commit();
        //            return true;
        //        }
        //        catch (Exception)
        //        {
        //            //事务回滚
        //            tran.Rollback();
        //            throw;
        //        }
        //    }
        //}
        ///// <summary>
        ///// 根据OrderSn查询ID
        ///// </summary>
        ///// <param name="OrderSn"></param>
        ///// <returns></returns>
        //private List<int> GetTShippingOrderDetailId(string OrderSn)
        //{
        //    List<int> listInt = new List<int>();

        //    var list = Context.TShippingOrderDetails.Where(x => x.OrderSn == OrderSn);

        //    if (list == null)
        //    {
        //        return listInt;
        //    }
        //    foreach (var item in list)
        //    {
        //        listInt.Add(item.Id);
        //    }
        //    return listInt;
        //}
        ///// <summary>
        ///// 一键选定(弃用)
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //public bool SelectTShippingOrderDetail(TShippingOrderDetail model)
        //{
        //    var OrderDetail = Context.TShippingOrderDetails.FirstOrDefault(m =>
        //        m.MaterialId == model.MaterialId &&
        //        m.WarehouseId == model.WarehouseId && m.BatchNo == model.BatchNo && m.Qty == model.Qty);
        //    if (OrderDetail == null)
        //    {
        //        return false; // 找不到对应的出库管理记录，选定失败
        //    }
        //    // 设置其他相关字段值，例如物料分类、物料名称、物料批次等

        //    OrderDetail.MaterialName = model.MaterialName;
        //    OrderDetail.WarehouseName = model.WarehouseName;
        //    OrderDetail.BatchNo = model.BatchNo;
        //    OrderDetail.Qty = model.Qty;
        //    OrderDetail.MaterialCode = model.MaterialCode;
        //    OrderDetail.StationId = model.StationId;
        //    OrderDetail.StationName = model.StationName;
        //    OrderDetail.OwnerId = model.OwnerId;
        //    OrderDetail.OwnerName = model.OwnerName;
        //    OrderDetail.AreaName = model.AreaName;
        //    OrderDetail.AreaCode = model.AreaCode;
        //    OrderDetail.ContractNo = model.ContractNo;
        //    OrderDetail.BillType = model.BillType;
        //    OrderDetail.DisOrderLineNo = model.DisOrderLineNo;
        //    OrderDetail.SupplierId = model.SupplierId;
        //    OrderDetail.SupplierName = model.SupplierName;
        //    OrderDetail.AuditUserId = model.AuditUserId;
        //    OrderDetail.AuditUserName = model.AuditUserName;
        //    OrderDetail.Status = model.Status;
        //    // 保存修改
        //    Context.SaveChanges();

        //    // 创建新的TShippingLockCell实例并赋值
        //    List<TShippingLockCell> lockCells = new List<TShippingLockCell>();

        //    //库位
        //    var Cells = Context.TCells.FirstOrDefault(m => lockCells.Any(x => x.CellId == m.Id));
        //    //出库详细单
        //    var orderDetail =
        //        Context.TShippingOrderDetails.FirstOrDefault(m => lockCells.Any(x => x.OrderDetailId == m.Id));
        //    //库存
        //    var inventory = Context.TInventories.FirstOrDefault(m => lockCells.Any(x => x.InventoryId == m.Id));

        //    if (new TShippingLockCell().InventoryQty >= model.Qty)
        //    {
        //        var lockCell = new TShippingLockCell
        //        {
        //            MaterialId = OrderDetail.MaterialId,
        //            MaterialCode = model.MaterialCode,
        //            MaterialName = model.MaterialName,
        //            OrderDetailId = model.Id,
        //            OutQty = OrderDetail.Qty,
        //            CellName = Cells.Name,
        //            WhId = OrderDetail.WarehouseId,
        //            InventoryId = inventory.Id,
        //            InventoryQty = inventory.AvailableQty,
        //            BatchNo = OrderDetail.BatchNo
        //        };
        //        lockCells.Add(lockCell);
        //    }
        //    else
        //    {
        //        // 根据出库数量创建多个TShippingLockCell实例并添加到列表中
        //        decimal? remainingQty = model.Qty;
        //        decimal? stockQuantity = inventory.AvailableQty;
        //        while (remainingQty > 0)
        //        {
        //            var lockCellss = new TShippingLockCell
        //            {
        //                MaterialId = OrderDetail.MaterialId,
        //                MaterialCode = model.MaterialCode,
        //                MaterialName = model.MaterialName,
        //                OrderDetailId = model.Id,
        //                OutQty = OrderDetail.Qty,
        //                CellName = Cells.Name,
        //                WhId = OrderDetail.WarehouseId,
        //                InventoryId = inventory.Id,
        //                BatchNo = OrderDetail.BatchNo,
        //                InventoryQty = Math.Min(stockQuantity ?? 0, remainingQty ?? 0),
        //            };

        //            lockCells.Add(lockCellss);

        //            remainingQty -= stockQuantity; // 更新剩余数量
        //        }
        //    }

        //    Context.TShippingLockCells.AddRange(lockCells);

        //    return Context.SaveChanges() > 0;
        //}
        ///// <summary>
        ///// 一键选定
        ///// </summary>
        ///// <param name="materid">物料Id</param>
        ///// <param name="count">出库数量</param>
        ///// <param name="whid">库房Id</param>
        ///// <param name="areaid">库区Id</param>
        ///// <param name="supid">供应商Id</param>
        ///// <param name="owid">货主Id</param>
        ///// <returns></returns>
        //public List<TInventory> GetAllTShippingLockCell(int materid, int count, int whid, int areaid, int supid,
        //    int owid)
        //{
        //    //var shippinglockcell=Context.TShippingLockCells.Where(x=>x.)
        //    var ilist = Context.TInventories.Where(x => x.MaterialId == materid);
        //    ilist = ilist.Where(x => x.AvailableQty != 0 && x.Status == 1);
        //    ilist = ilist.OrderBy(x => x.BatchNo);
        //    //库房
        //    if (whid != 0)
        //    {
        //        ilist = ilist.Where(x => x.WarehouseId == whid);
        //    }

        //    //库区
        //    if (areaid != 0)
        //    {
        //        ilist = ilist.Where(x => x.AreaId == areaid);
        //    }

        //    //供应商
        //    if (supid != 0)
        //    {
        //        ilist = ilist.Where(x => x.SupplierId == supid);
        //    }

        //    //货主
        //    if (owid != 0)
        //    {
        //        ilist = ilist.Where(x => x.OwnerId == owid);
        //    }

        //    List<TInventory> list = new List<TInventory>();
        //    //收货的数量
        //    foreach (var item in ilist)
        //    {
        //        //decimal? outqty = count - item.AvailableQty;
        //        if (count == item.AvailableQty)
        //        {
        //            //如果要出库的数量等于该库存的数量则直接返回
        //            item.OutQty = item.AvailableQty;
        //            list.Add(item);
        //            return list;
        //        }
        //        else if (count > item.AvailableQty)
        //        {
        //            item.OutQty = item.AvailableQty;
        //            list.Add(item);
        //            count = Convert.ToInt32(count - item.OutQty);
        //        }
        //        else if (count < item.AvailableQty)
        //        {
        //            item.OutQty = count;
        //            list.Add(item);
        //            return list;
        //        }
        //    }

        //    return list;
        //}
        ///// <summary>
        ///// 根据单号查询明细
        ///// </summary>
        ///// <param name="orderSn"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public List<TShippingOrderDetail> GetTShippingOrderDetailByOrderSn(string orderSn)
        //{
        //    var list = Context.TShippingOrderDetails
        //        .Where(x => x.OrderSn == orderSn).ToList();

        //    return list;
        //}
        ///// <summary>
        ///// 显示
        ///// </summary>
        ///// <param name="status"></param>
        ///// <param name="houseid"></param>
        ///// <param name="name"></param>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <param name="totalCount"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public List<TShippingOrderDetail> GetAllTShippingOrderDetail(int? billType, int? houseid, string? name,
        //    int pageIndex, int pageSize, out int totalCount)
        //{
        //    // 创建查询的基础列表，包含所有的 TShippingOrderDetail 记录
        //    var query = Context.TShippingOrderDetails.AsQueryable();

        //    // 根据传入的参数进行筛选
        //    if (billType.HasValue && billType.Value != 0)
        //    {
        //        query = query.Where(m => m.BillType == billType);
        //    }

        //    if (houseid.HasValue && houseid.Value != 0)
        //    {
        //        query = query.Where(m => m.WarehouseId == houseid);
        //    }

        //    if (!string.IsNullOrEmpty(name))
        //    {
        //        query = query.Where(m => m.MaterialName == name);
        //    }

        //    // 获取符合条件的总记录数
        //    totalCount = query.Count();

        //    // 对结果进行排序和分页
        //    var resultList = query.OrderByDescending(m => m.CreateDate)
        //        .Skip((pageIndex - 1) * pageSize)
        //        .Take(pageSize)
        //        .ToList();

        //    return resultList;
        //}

        ///// <summary>
        ///// 一键选定（弃用）
        ///// </summary>
        ///// <param name="qty"></param>
        ///// <param name="inventoryId"></param>
        ///// <param name="whId"></param>
        ///// <param name="materialId"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public List<TShippingLockCell> GetAllTShippingLockCell(TShippingOrderDetail model)
        //{
        //    // 校验物料Id是否为空或无效
        //    if (model.MaterialId <= 0)
        //    {
        //        throw new ArgumentException("请选择物料");
        //    }

        //    // 校验数量是否为空或无效
        //    if (model.Qty <= 0)
        //    {
        //        throw new ArgumentException("请选择出库数量");
        //    }

        //    if (model.WarehouseId <= 0)
        //    {
        //        throw new ArgumentException("请选择仓库");
        //    }

        //    if (model.MaterialId <= 0)
        //    {
        //        throw new ArgumentException("请选择物料");
        //    }

        //    List<TShippingLockCell> selectedCells = new List<TShippingLockCell>();
        //    // 查询仓位明细表中符合条件的数据
        //    var query = from cell in Context.TShippingLockCells
        //                where cell.InventoryQty >= model.Qty && cell.WhId == model.WarehouseId &&
        //                      cell.MaterialId == model.MaterialId

        //                select cell;

        //    // 遍历查询结果，并将符合条件的仓位明细添加到返回列表中
        //    foreach (var cell in query)
        //    {
        //        selectedCells.Add(cell);
        //    }

        //    return selectedCells;
        //}
        #endregion

    }
}